```html
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JPA事务管理深度解析 | 技术小馆</title>
    <link rel="stylesheet" href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            color: #333;
            line-height: 1.6;
        }
        .hero-gradient {
            background: linear-gradient(135deg, #6e8efb 0%, #a777e3 100%);
        }
        .card-hover {
            transition: all 0.3s ease;
        }
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 20px rgba(0,0,0,0.1);
        }
        .code-block {
            background: #2d3748;
            color: #f7fafc;
            border-radius: 0.5rem;
            overflow-x: auto;
        }
        .mermaid {
            background: white;
            padding: 1rem;
            border-radius: 0.5rem;
            box-shadow: 0 4px 6px rgba(0,0,0,0.05);
        }
        .drop-cap:first-letter {
            float: left;
            font-size: 4rem;
            line-height: 0.8;
            margin: 0.1em 0.2em 0 0;
            color: #6e8efb;
            font-weight: bold;
        }
    </style>
</head>
<body class="bg-gray-50">
    <!-- Hero Section -->
    <section class="hero-gradient text-white py-20 px-4 md:py-32">
        <div class="container mx-auto max-w-5xl text-center">
            <div class="flex justify-center mb-6">
                <span class="px-4 py-1 bg-white bg-opacity-20 rounded-full text-sm font-medium">JPA 深度解析</span>
            </div>
            <h1 class="text-4xl md:text-5xl font-bold mb-6 font-serif">JPA事务管理机制</h1>
            <p class="text-xl md:text-2xl opacity-90 max-w-3xl mx-auto">确保企业级应用数据一致性与完整性的核心策略</p>
            <div class="mt-12 flex justify-center space-x-4">
                <a href="#basic" class="px-6 py-3 bg-white text-indigo-600 font-medium rounded-lg hover:bg-opacity-90 transition">基本概念</a>
                <a href="#mechanism" class="px-6 py-3 border-2 border-white text-white font-medium rounded-lg hover:bg-white hover:bg-opacity-10 transition">管理机制</a>
            </div>
        </div>
    </section>

    <!-- Main Content -->
    <main class="container mx-auto max-w-5xl px-4 py-12 md:py-16">
        <!-- Intro Section -->
        <section class="mb-20">
            <p class="drop-cap text-lg md:text-xl leading-relaxed text-gray-700 mb-8">事务管理是任何企业级应用的核心部分，尤其是在处理数据库操作时。JPA（Java Persistence API）作为 Java EE 的一部分，提供了强大的事务管理机制，帮助开发者确保数据的一致性和完整性。</p>
            
            <div class="bg-blue-50 border-l-4 border-blue-500 p-6 rounded-r-lg mb-8">
                <div class="flex items-start">
                    <div class="flex-shrink-0 text-blue-500">
                        <i class="fas fa-info-circle text-xl"></i>
                    </div>
                    <div class="ml-3 text-blue-800">
                        <p><strong>关键点：</strong> JPA事务管理提供两种主要模式：容器管理事务(CMT)和应用管理事务(BMT)，每种模式适用于不同的应用场景。</p>
                    </div>
                </div>
            </div>
        </section>

        <!-- Basic Concepts -->
        <section id="basic" class="mb-20">
            <h2 class="text-3xl font-bold mb-6 text-gray-800 font-serif">1. 事务的基本概念</h2>
            
            <div class="grid md:grid-cols-2 gap-8 mb-12">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600">1.1 什么是事务？</h3>
                    <p class="mb-4 text-gray-700">事务是数据库操作的一个逻辑单元，包含一组操作，这些操作要么全部成功，要么全部失败。</p>
                    
                    <div class="space-y-4">
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 text-green-500">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-gray-700"><strong>原子性（Atomicity）</strong>：事务中的所有操作要么全部成功，要么全部失败。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 text-green-500">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-gray-700"><strong>一致性（Consistency）</strong>：事务执行前后，数据库的状态必须保持一致。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 text-green-500">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-gray-700"><strong>隔离性（Isolation）</strong>：多个事务并发执行时，一个事务的操作不应影响其他事务。</p>
                            </div>
                        </div>
                        <div class="flex items-start">
                            <div class="flex-shrink-0 mt-1 text-green-500">
                                <i class="fas fa-check-circle"></i>
                            </div>
                            <div class="ml-3">
                                <p class="text-gray-700"><strong>持久性（Durability）</strong>：事务一旦提交，其结果应永久保存在数据库中。</p>
                            </div>
                        </div>
                    </div>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <h3 class="text-xl font-semibold mb-4 text-indigo-600">1.2 事务的边界</h3>
                    <p class="mb-4 text-gray-700">事务的边界定义了事务的开始和结束。在 JPA 中，事务的边界通常由容器（如 Spring）或开发者显式管理。</p>
                    
                    <div class="bg-gray-100 p-4 rounded-lg">
                        <div class="flex items-center text-gray-700 mb-2">
                            <i class="fas fa-code-branch mr-2 text-indigo-500"></i>
                            <span>容器管理事务(CMT) - 由应用服务器自动管理</span>
                        </div>
                        <div class="flex items-center text-gray-700">
                            <i class="fas fa-user-cog mr-2 text-indigo-500"></i>
                            <span>应用管理事务(BMT) - 由开发者手动控制</span>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- ACID Visualization -->
            <div class="mermaid mb-12">
                pie title 事务四大特性(ACID)
                    "原子性(Atomicity)" : 25
                    "一致性(Consistency)" : 25
                    "隔离性(Isolation)" : 25
                    "持久性(Durability)" : 25
            </div>
        </section>

        <!-- JPA Transaction Mechanism -->
        <section id="mechanism" class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 font-serif">2. JPA 的事务管理机制</h2>
            
            <!-- CMT Section -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-12 card-hover">
                <div class="bg-indigo-600 text-white px-6 py-4">
                    <h3 class="text-xl font-semibold">2.1 容器管理的事务（CMT）</h3>
                    <p class="opacity-90">由应用服务器（如 Java EE 容器）自动管理的事务</p>
                </div>
                <div class="p-6">
                    <div class="mb-6">
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">2.1.1 事务注解</h4>
                        <div class="grid md:grid-cols-2 gap-4">
                            <div class="bg-indigo-50 p-4 rounded-lg">
                                <div class="text-indigo-700 font-medium mb-2"><i class="fas fa-tag mr-2"></i>@Transactional</div>
                                <p class="text-gray-700">声明一个方法或类需要事务管理</p>
                            </div>
                            <div class="bg-indigo-50 p-4 rounded-lg">
                                <div class="text-indigo-700 font-medium mb-2"><i class="fas fa-tag mr-2"></i>@TransactionAttribute</div>
                                <p class="text-gray-700">指定事务的属性，如 REQUIRED、REQUIRES_NEW 等</p>
                            </div>
                        </div>
                    </div>
                    
                    <div class="mb-6">
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">2.1.2 事务属性</h4>
                        <div class="overflow-x-auto">
                            <table class="min-w-full bg-gray-100 rounded-lg overflow-hidden">
                                <thead>
                                    <tr class="bg-gray-200 text-gray-700">
                                        <th class="px-4 py-2 text-left">属性</th>
                                        <th class="px-4 py-2 text-left">描述</th>
                                    </tr>
                                </thead>
                                <tbody class="text-gray-700">
                                    <tr class="border-b border-gray-200 hover:bg-gray-50">
                                        <td class="px-4 py-3 font-medium">REQUIRED</td>
                                        <td class="px-4 py-3">如果当前存在事务，则加入该事务；否则，创建一个新事务</td>
                                    </tr>
                                    <tr class="border-b border-gray-200 hover:bg-gray-50">
                                        <td class="px-4 py-3 font-medium">REQUIRES_NEW</td>
                                        <td class="px-4 py-3">总是创建一个新事务，如果当前存在事务，则挂起该事务</td>
                                    </tr>
                                    <tr class="border-b border-gray-200 hover:bg-gray-50">
                                        <td class="px-4 py-3 font-medium">SUPPORTS</td>
                                        <td class="px-4 py-3">如果当前存在事务，则加入该事务；否则，以非事务方式执行</td>
                                    </tr>
                                    <tr class="border-b border-gray-200 hover:bg-gray-50">
                                        <td class="px-4 py-3 font-medium">NOT_SUPPORTED</td>
                                        <td class="px-4 py-3">以非事务方式执行，如果当前存在事务，则挂起该事务</td>
                                    </tr>
                                    <tr class="border-b border-gray-200 hover:bg-gray-50">
                                        <td class="px-4 py-3 font-medium">MANDATORY</td>
                                        <td class="px-4 py-3">如果当前存在事务，则加入该事务；否则，抛出异常</td>
                                    </tr>
                                    <tr class="hover:bg-gray-50">
                                        <td class="px-4 py-3 font-medium">NEVER</td>
                                        <td class="px-4 py-3">以非事务方式执行，如果当前存在事务，则抛出异常</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    
                    <div>
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">2.1.3 示例</h4>
                        <p class="mb-4 text-gray-700">在 <code class="bg-gray-100 px-2 py-1 rounded">ts-service</code> 项目中，团队使用容器管理的事务处理用户注册逻辑：</p>
                        <div class="code-block p-4 rounded-lg mb-6">
                            <pre class="text-sm"><code class="language-java">@Stateless
public class UserService {

    @PersistenceContext
    private EntityManager em;

    @Transactional(REQUIRED)
    public void registerUser(User user) {
        em.persist(user);
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- BMT Section -->
            <div class="bg-white rounded-xl shadow-md overflow-hidden mb-12 card-hover">
                <div class="bg-purple-600 text-white px-6 py-4">
                    <h3 class="text-xl font-semibold">2.2 应用管理的事务（BMT）</h3>
                    <p class="opacity-90">由开发者显式管理的事务</p>
                </div>
                <div class="p-6">
                    <div class="mb-6">
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">2.2.1 UserTransaction 接口</h4>
                        <div class="grid md:grid-cols-2 gap-4">
                            <div class="bg-purple-50 p-4 rounded-lg">
                                <div class="text-purple-700 font-medium mb-2"><i class="fas fa-play-circle mr-2"></i>begin()</div>
                                <p class="text-gray-700">开始一个新事务</p>
                            </div>
                            <div class="bg-purple-50 p-4 rounded-lg">
                                <div class="text-purple-700 font-medium mb-2"><i class="fas fa-check-circle mr-2"></i>commit()</div>
                                <p class="text-gray-700">提交当前事务</p>
                            </div>
                            <div class="bg-purple-50 p-4 rounded-lg">
                                <div class="text-purple-700 font-medium mb-2"><i class="fas fa-undo-alt mr-2"></i>rollback()</div>
                                <p class="text-gray-700">回滚当前事务</p>
                            </div>
                            <div class="bg-purple-50 p-4 rounded-lg">
                                <div class="text-purple-700 font-medium mb-2"><i class="fas fa-flag mr-2"></i>setRollbackOnly()</div>
                                <p class="text-gray-700">标记当前事务为回滚状态</p>
                            </div>
                        </div>
                    </div>
                    
                    <div>
                        <h4 class="text-lg font-semibold mb-3 text-gray-700">2.2.2 示例</h4>
                        <p class="mb-4 text-gray-700">在 <code class="bg-gray-100 px-2 py-1 rounded">ts-service</code> 项目中，团队使用应用管理的事务处理订单创建逻辑：</p>
                        <div class="code-block p-4 rounded-lg mb-6">
                            <pre class="text-sm"><code class="language-java">@Stateless
public class OrderService {

    @PersistenceContext
    private EntityManager em;

    @Resource
    private UserTransaction utx;

    public void createOrder(Order order) {
        try {
            utx.begin();
            em.persist(order);
            utx.commit();
        } catch (Exception e) {
            try {
                utx.rollback();
            } catch (SystemException se) {
                throw new RuntimeException(se);
            }
            throw new RuntimeException(e);
        }
    }
}</code></pre>
                        </div>
                    </div>
                </div>
            </div>
            
            <!-- Propagation Behavior -->
            <div class="mb-12">
                <h3 class="text-2xl font-semibold mb-6 text-gray-800">2.3 事务的传播行为</h3>
                <p class="mb-6 text-gray-700">事务的传播行为定义了事务在方法调用之间的传播方式。</p>
                
                <div class="mermaid">
                    flowchart TD
                        A[调用方法] -->|REQUIRED| B[有事务?]
                        B -->|是| C[加入事务]
                        B -->|否| D[创建新事务]
                        A -->|REQUIRES_NEW| E[创建新事务]
                        A -->|NESTED| F[有事务?]
                        F -->|是| G[创建嵌套事务]
                        F -->|否| H[创建新事务]
                </div>
            </div>
            
            <!-- Isolation Levels -->
            <div>
                <h3 class="text-2xl font-semibold mb-6 text-gray-800">2.4 事务的隔离级别</h3>
                <p class="mb-6 text-gray-700">事务的隔离级别定义了事务之间的可见性。</p>
                
                <div class="grid md:grid-cols-4 gap-4">
                    <div class="bg-white p-4 rounded-lg shadow-sm border border-gray-200">
                        <div class="text-sm font-semibold text-indigo-600 mb-2">READ_UNCOMMITTED</div>
                        <p class="text-sm text-gray-600">最低隔离级别，允许读取未提交的数据</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-sm border border-gray-200">
                        <div class="text-sm font-semibold text-indigo-600 mb-2">READ_COMMITTED</div>
                        <p class="text-sm text-gray-600">保证读取的数据是已提交的</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-sm border border-gray-200">
                        <div class="text-sm font-semibold text-indigo-600 mb-2">REPEATABLE_READ</div>
                        <p class="text-sm text-gray-600">保证同一事务中多次读取的数据一致</p>
                    </div>
                    <div class="bg-white p-4 rounded-lg shadow-sm border border-gray-200">
                        <div class="text-sm font-semibold text-indigo-600 mb-2">SERIALIZABLE</div>
                        <p class="text-sm text-gray-600">最高隔离级别，保证事务完全隔离</p>
                    </div>
                </div>
            </div>
        </section>
        
        <!-- Best Practices -->
        <section class="mb-20">
            <h2 class="text-3xl font-bold mb-8 text-gray-800 font-serif">3. 最佳实践</h2>
            
            <div class="grid md:grid-cols-3 gap-8">
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="text-indigo-500 text-3xl mb-4">
                        <i class="fas fa-tags"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">3.1 合理使用事务注解</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>避免过度使用事务，只在需要的方法或类上使用</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>根据业务需求选择合适的事务属性</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="text-indigo-500 text-3xl mb-4">
                        <i class="fas fa-tachometer-alt"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">3.2 优化事务性能</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>尽量缩小事务的范围，减少持有时间</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>通过批量处理减少数据库操作次数</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>根据需求调整事务隔离级别</span>
                        </li>
                    </ul>
                </div>
                
                <div class="bg-white p-6 rounded-xl shadow-md card-hover">
                    <div class="text-indigo-500 text-3xl mb-4">
                        <i class="fas fa-exclamation-triangle"></i>
                    </div>
                    <h3 class="text-xl font-semibold mb-3 text-gray-800">3.3 处理事务异常</h3>
                    <ul class="space-y-2 text-gray-700">
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>在事务中捕获并处理异常</span>
                        </li>
                        <li class="flex items-start">
                            <i class="fas fa-check-circle text-green-500 mt-1 mr-2"></i>
                            <span>使用setRollbackOnly标记事务为回滚状态</span>
                        </li>
                    </ul>
                </div>
            </div>
        </section>
        
        <!-- Summary -->
        <section class="bg-indigo-50 rounded-xl p-8 mb-12">
            <div class="flex items-start">
                <div class="flex-shrink-0 text-indigo-500 text-2xl mt-1 mr-4">
                    <i class="fas fa-lightbulb"></i>
                </div>
                <div>
                    <h3 class="text-xl font-semibold mb-3 text-indigo-800">关键总结</h3>
                    <p class="text-gray-700 mb-4">JPA提供了强大的事务管理机制，包括容器管理的事务(CMT)和应用管理的事务(BMT)，开发者应根据具体场景选择合适的模式。</p>
                    <p class="text-gray-700">遵循事务最佳实践，包括合理使用注解、优化性能和处理异常，可以确保应用的数据一致性和高性能。</p>
                </div>
            </div>
        </section>
    </main>
    
    <!-- Footer -->
    <footer class="bg-gray-900 text-gray-300 py-8">
        <div class="container mx-auto px-4 max-w-5xl">
            <div class="flex flex-col md:flex-row justify-between items-center">
                <div class="mb-4 md:mb-0">
                    <p class="text-xl font-semibold text-white">技术小馆</p>
                    <p class="text-gray-400 mt-1">分享前沿技术，传播开发智慧</p>
                </div>
                <div>
                    <a href="http://www.yuque.com/jtostring" class="text-blue-400 hover:text-blue-300 transition duration-200 flex items-center">
                        <i class="fas fa-globe mr-2"></i> http://www.yuque.com/jtostring
                    </a>
                </div>
            </div>
            <div class="border-t border-gray-800 mt-6 pt-6 text-sm text-gray-500 text-center">
                &copy; 2023 技术小馆. 保留所有权利.
            </div>
        </div>
    </footer>
    
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <script>
        mermaid.initialize({
            startOnLoad: true,
            theme: 'default',
            flowchart: {
                useMaxWidth: true,
                htmlLabels: true,
                curve: 'basis'
            }
        });
        
        // Simple scroll animation for internal links
        document.querySelectorAll('a[href^="#"]').forEach(anchor => {
            anchor.addEventListener('click', function (e) {
                e.preventDefault();
                document.querySelector(this.getAttribute('href')).scrollIntoView({
                    behavior: 'smooth'
                });
            });
        });
    </script>
</body>
</html>
```